package com.sttk.secreconeclick.bookkeep.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.StatisticsAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.bean.YearlyStatisticsBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityStatisticsBinding;
import com.sttk.secreconeclick.bookkeep.dialog.SelYearDialog;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;
import com.sttk.secreconeclick.bookkeep.utils.MyBillReader;

import java.text.SimpleDateFormat;
import java.util.List;

public class StatisticsActivity extends BaseAc<ActivityStatisticsBinding> implements View.OnClickListener {

    private StatisticsAdapter statisticsAdapter;
    private SelYearDialog selYearDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_statistics;
    }

    @Override
    protected void initView() {
        mDataBinding.ivStatisticsBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvYear.setOnClickListener(this);

        mDataBinding.rvStatisticsList.setLayoutManager(new LinearLayoutManager(mContext));
        statisticsAdapter = new StatisticsAdapter();
        mDataBinding.rvStatisticsList.setAdapter(statisticsAdapter);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvYear) {//年份
            showSelYearDialog();
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvYear.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy年")));
        String[] split = mDataBinding.tvYear.getText().toString().split("年");
        getData(Integer.parseInt(split[0]));
    }

    private void getData(int year) {
        AccountBookBean accountBookBean = AppSpUtils.getAccountBookList().get(SPUtil.getInt(mContext, Content.accountBook, 0));
        List<YearlyStatisticsBean> listData = MyBillReader.getYearlyStatistics(accountBookBean.getBillBeanList(), year);
        statisticsAdapter.setList(listData);

        // 计算年度总收入、总支出和总结余
        double totalIncome = 0.0;
        double totalExpenditure = 0.0;
        double totalBalance = 0.0;

        for (YearlyStatisticsBean bean : listData) {
            totalIncome += bean.getIncome();
            totalExpenditure += bean.getExpenditure();
            totalBalance += bean.getBalance();
        }

        // 更新UI显示，保留两位小数
        mDataBinding.tvExpenditure.setText(String.format("%.2f", totalExpenditure));
        mDataBinding.tvIncome.setText(String.format("%.2f", totalIncome));
        mDataBinding.tvTotalBalance.setText(String.format("%.2f", totalBalance));
    }

    private void showSelYearDialog() {
        if (selYearDialog == null) {
            selYearDialog = new SelYearDialog(mContext);
            selYearDialog.setDate(mDataBinding.tvYear.getText().toString());
        }
        selYearDialog.setListener(new SelYearDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                mDataBinding.tvYear.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy年")));
                String[] split = mDataBinding.tvYear.getText().toString().split("年");
                getData(Integer.parseInt(split[0]));
            }
        });
        selYearDialog.show();
    }

}